package com.zidongxiangxi.rabbit.core.util;

import com.zidongxiangxi.rabbit.core.constant.RabbitHeaderConstants;
import com.zidongxiangxi.rabbit.core.constant.enums.MessageTypeEnum;
import org.springframework.amqp.core.Message;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.Objects;

/**
 * RabbitMQ工具类
 *
 * @author chenxudong
 * @date 2020/11/07
 */
public class RabbitUtils {
	public static boolean isSequenceMessage(Message message) {
		Integer messageType = getMessageType(message);
		String messageGroupName = getMessageGroupName(message);
		return Objects.equals(messageType, MessageTypeEnum.SEQUENCE.getValue()) && Objects.nonNull(messageGroupName);
	}

	public static void setMessageHeader(Message message, String header, Object value) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())
				|| Objects.isNull(value)) {
			return;
		}
		message.getMessageProperties().setHeader(header, value);
	}

	public static Object getMessageHeader(Message message, String header) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())) {
			return null;
		}
		return message.getMessageProperties().getHeaders().get(header);
	}

	public static String getMessageId(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())) {
			return null;
		}
		return message.getMessageProperties().getMessageId();
	}

	public static String getProduceApplication(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())) {
			return null;
		}
		return message.getMessageProperties().getAppId();
	}

	public static Integer getMessageType(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())
				|| CollectionUtils.isEmpty(message.getMessageProperties().getHeaders())
				|| !message.getMessageProperties().getHeaders().containsKey(RabbitHeaderConstants.MESSAGE_TYPE)) {
			return null;
		}
		Object messageType = message.getMessageProperties().getHeaders().get(RabbitHeaderConstants.MESSAGE_TYPE);
		if (!(messageType instanceof Number)) {
			return null;
		}
		return ((Number)messageType).intValue();
	}

	public static String getMessageGroupName(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())
				|| CollectionUtils.isEmpty(message.getMessageProperties().getHeaders())
				|| !message.getMessageProperties().getHeaders().containsKey(RabbitHeaderConstants.MESSAGE_GROUP_NAME)) {
			return null;
		}
		return message.getMessageProperties().getHeaders().get(RabbitHeaderConstants.MESSAGE_GROUP_NAME).toString();
	}

	public static String getPreviousMessageId(Message message) {
		Object previousMessageId = getMessageHeader(message, RabbitHeaderConstants.MESSAGE_PREVIOUS_MESSAGE_ID);
		if (previousMessageId instanceof String) {
			return (String)previousMessageId;
		}
		return null;
	}

	public static String getVirtualHost(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())
				|| CollectionUtils.isEmpty(message.getMessageProperties().getHeaders())
				|| !message.getMessageProperties().getHeaders().containsKey(RabbitHeaderConstants.MESSAGE_VIRTUAL_HOST)) {
			return null;
		}
		return message.getMessageProperties().getHeaders().get(RabbitHeaderConstants.MESSAGE_VIRTUAL_HOST).toString();
	}

	public static Date getTimestamp(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())) {
			return null;
		}
		return message.getMessageProperties().getTimestamp();
	}

	public static String getConsumerQueue(Message message) {
		if (Objects.isNull(message)
				|| Objects.isNull(message.getMessageProperties())) {
			return null;
		}
		return message.getMessageProperties().getConsumerQueue();
	}

	public static void setProduceRecordId(Message message, Long produceRecordId) {
		message.getMessageProperties().setHeader(RabbitHeaderConstants.MESSAGE_PRODUCE_RECORD_ID, produceRecordId);
	}

	public static void setPreviousMessageId(Message message, String previousMessageId) {
		message.getMessageProperties().setHeader(RabbitHeaderConstants.MESSAGE_PREVIOUS_MESSAGE_ID, previousMessageId);
	}
}
